<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="创建时间" prop="createDate">
        <el-date-picker clearable
          v-model="queryParams.createDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['factory:factory:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['factory:factory:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['factory:factory:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['factory:factory:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-container>
      <el-aside width="200px" style="background-color: rgb(238, 241, 246)">
        <el-tree
          :data="treeData"
          node-key="id"
          @node-click="fetchTableData"
          default-expand-all
          accordion
        ></el-tree>
      </el-aside>

      <el-container>
          <el-table v-loading="loading" :data="factoryList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="创建时间" align="center" prop="createDate" width="180">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.createDate, '{y}-{m}-{d}') }}</span>
              </template>
            </el-table-column>
            <el-table-column label="编号" align="center" prop="num" />
            <el-table-column label="名称" align="center" prop="name" />
            <el-table-column label="生产线名称" align="center" prop="lineName" v-if="showtable"/>
            <el-table-column label="车间名称" align="center" prop="workshopName" v-if="showtable2"/>
            <el-table-column label="工厂名称" align="center" prop="factoryName" v-if="showtable3"/>
            <el-table-column label="工厂地址" align="center" prop="address" v-if="showtable4"/>
            <el-table-column label="备注" align="center" prop="memo" />
            <el-table-column label="创建人" align="center" prop="createName" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdate(scope.row)"
                  v-hasPermi="['factory:factory:edit']"
                >修改</el-button>
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelete(scope.row)"
                  v-hasPermi="['factory:factory:remove']"
                >删除</el-button>
              </template>
            </el-table-column>
          </el-table>
      </el-container>
    </el-container>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body v-if="showtable4">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="工厂编号" prop="num">
          <el-input v-model="form.num" placeholder="请输入编号" />
        </el-form-item>
        <el-form-item label="工厂名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="工厂地址" prop="address" >
          <el-input v-model="form.address" placeholder="请输入工厂地址" />
        </el-form-item>
        <el-form-item label="备注" prop="memo">
          <el-input v-model="form.memo" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body v-if="showtable3">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="车间编号" prop="num">
          <el-input v-model="form.num" placeholder="请输入编号" />
        </el-form-item>
        <el-form-item label="车间名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="所属工厂" prop="factoryId">
          <el-select v-model="form.factoryId" placeholder="请选择工厂">
            <el-option v-for="item in factoryaddList" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="memo">
          <el-input v-model="form.memo" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body v-if="showtable2">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="所属工厂" prop="factoryId">
          <el-select v-model="form.factoryId" placeholder="请选择工厂">
            <el-option v-for="item in factoryaddList" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属车间" prop="workshopId">
          <el-select v-model="form.workshopId" placeholder="请选择车间">
            <el-option v-for="item in workshopList" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="生产线编号" prop="num">
          <el-input v-model="form.num" placeholder="请输入编号" />
        </el-form-item>
        <el-form-item label="生产线名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="备注" prop="memo">
          <el-input v-model="form.memo" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body v-if="showtable">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="所属工厂" prop="factoryId">
          <el-select v-model="form.factoryId" placeholder="请选择工厂">
            <el-option v-for="item in factoryaddList" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属车间" prop="workshopId">
          <el-select v-model="form.workshopId" placeholder="请选择车间">
            <el-option v-for="item in workshopList" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属生产线" prop="lineId">
          <el-select v-model="form.lineId" placeholder="请选择生产线">
            <el-option v-for="item in linkList" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="工位编号" prop="num">
          <el-input v-model="form.num" placeholder="请输入编号" />
        </el-form-item>
        <el-form-item label="工位名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="备注" prop="memo">
          <el-input v-model="form.memo" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listFactory, getFactory, delFactory, addFactory, updateFactory,ListTreeData,getFactoryList } from "@/api/factory/factory";
import axios from "axios";

export default {
  name: "Factory",
  data() {
    return {
      treeData: [
        { id: 1, label: "请选择", children: [] },
      ],
      // 添加表格是否显示
      showtable: false,
      showtable2: false,
      showtable3: false,
      showtable4: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      //表格数据
      factoryList: [],
      //工厂列表
      factoryaddList:[],
      //车间列表
      workshopList:[],
      //生产线列表
      linkList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        linkage: null,
        createDate: null,
        num: null,
        name: null,
        factoryId: null,
        workshopId: null,
        lineId: null,
        address: null,
        memo: null,
        createName: null,
        isDelete: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      linkage: 0,
      type: 0,
    };
  },
  created() {
    this.getList()
    this.getTreeData();
    this.fetchTableData(this.treeData[0]);
  },
  methods: {
    //获取树形结构数据
    getTreeData() {
      // 假设有一个API可以获取树形结构数据
      ListTreeData().then(response => {
        this.treeData = response.data;
      }).catch(error => {
        console.error('数据获取失败:', error);
      });
    },
    // 通过树节点的id获取对应的表格数据
    fetchTableData(node) {
      this.loading = true;
      // 假设有一个API可以根据node.id获取相应的数据
      getFactoryList(node.id).then(response => {
        this.factoryList = response.data;
        this.linkage = this.factoryList[0].linkage;
        this.type = this.factoryList[0].type;
        this.showtable = this.factoryList[0].lineId != null;
        this.showtable2 = this.factoryList[0].workshopName != null;
        this.showtable3 = this.factoryList[0].factoryId != null;
        this.showtable4 = this.factoryList[0].address != null;
        this.loading = false;
      }).catch(error => {
        console.error('数据获取失败:', error);
        this.loading = false;
      });
    },
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      listFactory(this.queryParams).then(response => {
        this.factoryList = response.rows;
        for (let i = 0; i < this.factoryList.length; i++) {
          if (this.factoryList[i].type == 1) {
              this.factoryaddList.push(this.factoryList[i])
          }
          if (this.factoryList[i].type == 2) {
            this.workshopList.push(this.factoryList[i])
          }
          if (this.factoryList[i].type == 3) {
            this.linkList.push(this.factoryList[i])
          }
        }
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        linkage: null,
        createDate: null,
        num: null,
        name: null,
        factoryId: null,
        workshopId: null,
        lineId: null,
        address: null,
        memo: null,
        createName: null,
        isDelete: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getFactory(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateFactory(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.linkage = this.linkage;
            this.form.type = this.type;
            addFactory(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除编号为"' + ids + '"的数据项？').then(function() {
        return delFactory(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('factory/factory/export', {
        ...this.queryParams
      }, `factory_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
